<?php
  //引入系统配置文件
  include_once('config/init.php');
  include_once('check.php');

  // 接收用户id
  $id = isset($_GET["id"]) ? trim($_GET["id"]) : 0;
  // var_dump($id);
  // exit;

  // 根据id去查询管理员是否存在
  $sql = "SELECT * FROM {$pre_}admin WHERE id=$id";
  $admin = find($sql);
  // var_dump($admin);
  // exit;

  // 当编辑的管理员不存在时
  if(!$admin){
    Notice("当前编辑管理员不存在");
    exit;
  }


  if($_POST){
    // var_dump($_POST);
    // exit;

    $password = isset($_POST['password']) ? trim($_POST['password']) : '';
    
    if(!empty($password)){
      $salt = randstr();
      $password = md5($password.$salt);

      // 组装数据
      $data = [
        'username' => isset($_POST['username']) ? trim($_POST['username']) : '',
        'password' => $password,
        'salt' => $salt
      ];
    }else{
        // 组装数据
        $data = [
          'username' => isset($_POST['username']) ? trim($_POST['username']) : ''
        ];
    }

    


    // 头像判断是否有图片上传
    if($_FILES['avatar']['error'] == 0 && $_FILES['avatar']['size'] > 0){
      $path = 'assets/uploads/';
      $success = Upload('avatar',$path);
      
      // 如果上传不成功
      if(!$success['result']){
        Notice($success['msg']);
        exit;
      }
      // 直接将路径放到数据中
      $data['avatar'] = $success['data'];
    }

    
    $res = edit("admin",$data,"id=$id");
    // var_dump($res);
    // exit;
    // 判断是否更新成功
    if($res){
      // 如果更新图片就要把之前的图片删掉
      if(isset($data['avatar'])){
        // 如果有图片上传，就要判断之前是否有图片
        if(!empty($admin['avatar']) && is_file("./".$admin['avatar'])){
          @unlink('./'.$admin['avatar']);
        }
      }

      Notice("编辑管理员成功","AdminList.php");
      exit;
    }else{
      Notice("编辑员工失败");
      exit;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
   
     <!-- 引入公共样式 -->
     <?php include_once('meta.php');?>

     <style>

        .preview{
          width:170px;
          height: 170px;
          overflow: hidden;
          margin-top:5px;
        }
        .preview img{
          width:100%;
        }
    </style>
  </head>

  <body>

   <!-- 引入头部 -->
   <?php include_once('header.php');?>

    <!-- 引入菜单 -->
    <?php include_once('menu.php');?>
    

    <div class="content">
        <div class="header">
            <h1 class="page-title">编辑管理员</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">编辑管理员</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                    
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='AdminList.php'"><i class="icon-list"></i> 返回管理员管理</button>
                  <div class="btn-group">
                  </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                      <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">
                            <label>管理员姓名</label>
                            <input type="text" name="username" required value="<?php echo $admin['username']; ?>" placeholder="请输入管理员姓名" class="input-xxlarge" >
                       
                            <label>管理员密码</label>
                            <input type="password" name="password" value="" placeholder="如果要更改密码，请输入管理员新密码" class="input-xxlarge" >
                            
                            <label>管理员头像</label>
                            <input type="file" name="avatar" id="avatar">
                            <div class="preview">
                              <?php if(!empty($admin['avatar']) && is_file("./".$admin['avatar'])){ ?>
                              <img src="./<?php echo $admin['avatar'];?>">
                              <?php }else{ ?>
                              <img src="./assets/img/170x170.gif" alt="">
                              <?php } ?>
                            </div>
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交" />
                        </form>
                      </div>
                  </div>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                  </div>
                  <div class="modal-body">
                    
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                  </div>
                  <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                  </div>
                </div>
                
                <footer>
                    <hr>
                    <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                </footer>
                    
            </div>
        </div>
    </div>
  </body>
</html>
<script>

  // 给图片绑定一个改变事件
  $("#avatar").change(function(){
    // console.log($(this)[0].files[0]);

    var avatar = $(this)[0].files[0] ? $(this)[0].files[0] : null;

    // 如果没有选择图片
    if(!avatar){
      return;
    }

    // 创建一个读取器
    var reader = new FileReader();

    // console.log(reader);

    // 加载文件
    reader.readAsDataURL(avatar);

    // 触发加载成功的事件
    reader.onload = function(e){
      // 追加图片
      // console.log(e.target.result);

      $(".preview").html(`<img src="${e.target.result}">`)
    }
  })
</script>